<?php

/**
 * Create a new object based on an existing one, allowing to add new properties and methods
 */
class ObjectDecorator
{
    /* original object */
    protected $_;

    public function __construct($object)
    {
        $this->_ = $object;
    }

    public function __get($property)
    {
        return $this->_->$property;
    }

    public function __set($property, $value)
    {
        if (property_exists($this->_, $property))
        {
            $this->_->property = $value;
        }
        else
        {
            $this->property = $value;
        }
    }

    public function __call($method, $arguments)
    {
        if (method_exists($this->_, $method))
        {
            return call_user_func_array(array($this->_, $method), $arguments);
        }

        trigger_error('Call to undefined method ' . get_called_class() . '::' . $method . '()', E_USER_ERROR);
    }

}
